import * as qs from "qs";

function awaitHttpGet(url, params, option = {}){
    return new Promise((resolve, reject) => {
        let ajax = new XMLHttpRequest();
        if (typeof params !== 'string') {
            params = qs.stringify(params)
        }
        if (params) {
            params = `&${params}`
        }
        ajax.open('get', `${url}${params}`);
        ajax.onreadystatechange = () => {
            if (ajax.readyState === 4 && ajax.status === 200) {
                resolve(JSON.parse(ajax.responseText));
            }
        };
        ajax.send();
    });
}

function debug_change_url (url) {
    var res = url.replace(/127.0.0.1/g, "weixin.pbani.com");
    return res
}

export default async function (arr){
    let tasks = [];
    for (let doc of arr) {
        let url = debug_change_url(doc.url);
        console.log(url)

        let param = doc.param;
        let method = doc.method;
        let option = doc.option || {};

        if (method && method === 'post') {
        } else {
            tasks.push(awaitHttpGet(url, param, option));
        }
    }

    let res = await Promise.all(tasks);

    if (res.length === 1) {
        return res[0];
    } else {
        return res;
    }
}
